package com.example.intelligentattendancesystem.service.impl;

import com.example.intelligentattendancesystem.Utils.UserTool;
import com.example.intelligentattendancesystem.mapping.AbsenceMapping;
import com.example.intelligentattendancesystem.pojo.Absence;
import com.example.intelligentattendancesystem.pojo.History;
import com.example.intelligentattendancesystem.pojo.User;
import com.example.intelligentattendancesystem.service.AbsenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @version 1.0
 * @className AbsenceServiceImpl
 * @since 1.0
 */
@Service
public class AbsenceServiceImpl implements AbsenceService {
    @Autowired
    AbsenceMapping absenceMapping;
    @Autowired
    UserTool userTool;
    @Override
    public List<Absence> getAllAbsenceService() {
        List<Absence> absences = absenceMapping.selectAllAbsence();
        return absences;
    }

    @Override
    public boolean addAbsenceService(int userId, String startTime, String endTime, String currentTime) {
        Integer id=null;
        String absenceStartTime=currentTime.split(" ")[0]+" "+startTime;
        String absenceEndTime=currentTime.split(" ")[0]+" "+endTime;
        Absence absence = new Absence(id, userId, absenceStartTime, absenceEndTime);
        //判断是否已经存在
        List<Absence> absences = absenceMapping.selectAbsenceByUserId(userId);
        for (int j = 0; j < absences.size(); j++) {
            if(absences.get(j).getUserId()==absence.getUserId() && absences.get(j).getAbsenceStartTime().equals(absence.getAbsenceStartTime())){
                //若存在直接返回false
                return false;
            }
        }
        int i=0;
        i = absenceMapping.insertAbsence(absence);
        if(i==0)
            return false;
        return true;
    }

    @Override
    public List<Absence> getAbsenceService(int userId) {
        List<Absence> absences = absenceMapping.selectAbsenceByUserId(userId);
//        List<Absence> absences1 = new ArrayList<>();
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String currentTime=df.format(new Date());
//        //将本月记录找出来
//        String curDay = currentTime.split(" ")[0];
//        //月份，找preDay-curDay的记录
//        String preDay = curDay.split("-")[0]+"-"+curDay.split("-")[1]+"-00";
//        System.out.println(preDay+"--"+curDay);
//        for (int i = 0; i < absences.size(); i++) {
//            if(absences.get(i).getAbsenceStartTime().split(" ")[0].compareTo(curDay)<=0 && absences.get(i).getAbsenceStartTime().split(" ")[0].compareTo(preDay)>=0){
//                absences1.add(absences.get(i));
//            }
//        }
        return absences;
    }

    @Override
    public List<User> getAbsUsers() {
        List<Absence> allAbsenceService = getAllAbsenceService();
        List<Integer> ids = new ArrayList<>();
        ids.add(allAbsenceService.get(0).getUserId());
        int flag=0;
        for (Absence a : allAbsenceService) {
            flag=0;
            for (Integer id : ids) {
                if (a.getUserId() == id) {
                    flag=1;
                }
            }
            if(flag==0){
                ids.add(a.getUserId());
            }
        }
        List<User> usersByUserIds = userTool.getUsersByUserIds(ids);
        return usersByUserIds;
    }
}
